package com.zqxq.lottery.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.vo.sys.ChannelDataExportVo;
import com.zqxq.lottery.service.sys.ISysAnalyzeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 苦智
 * @date 2018/4/9 14:25
 * @description 数据分析统计
 * @ClassName SysDataAnalyzeController
 */
@RestController
@Slf4j
@RequestMapping("/data")
public class SysDataAnalyzeController extends SysBaseController{

    @Autowired
    private ISysAnalyzeService sysAnalyzeService;

    @PostMapping("/dayStatistics")
    public Result getDataCount(String startTime, String endTime){
        Map<String,Object> resultMap = new HashMap<>();
        //数据总的统计量
        Map<String,Object> totalMap = sysAnalyzeService.getTotalCount(startTime,endTime);
        resultMap.put("totalCount",totalMap);
        //初始化时间
        if (StrUtils.isEmpty(startTime) || StrUtils.isEmpty(endTime)){
            //初始查询化时间
            endTime = DateUtils.getCurrentDateStr();
            startTime = DateUtils.dateToStr(DateUtils.addDays(new Date(),-30),"yyyy-MM-dd");
        }
        //数据统计K线
        List<Map<String,Object>> mapList = sysAnalyzeService.getKLineCount(startTime,endTime);
        resultMap.put("totalKLine",mapList);
        //详细数据统计
        Map<String,Object> eachMap = sysAnalyzeService.getEachCount();
        resultMap.put("eachCount",eachMap);
        return new Result(CodeEnum.SUCCESS.getCode(),"统计成功",resultMap);
    }

    @PostMapping("/getChannelCount")
    public Result getChannelCount(@RequestBody Map<String,Object> paramMap){
        log.info("后台管理渠道统计 paramMap={}", JSONObject.toJSONString(paramMap));
        Map<String, Object> channelCount = sysAnalyzeService.getChannelCount(paramMap);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),channelCount);
    }

    @PostMapping("/getAllChannel")
    public Result getAllChannel(){
        log.info("查询所有渠道");
        List<String> resultList = sysAnalyzeService.getAllChannel();
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),resultList);
    }

    //查询渠道统计列表
    @PostMapping("/getChannelCountList")
    public Result getChannelCountList(@RequestBody Map<String,Object> paramMap){
        log.info("后台管理渠道统计导出 paramMap={}", JSONObject.toJSONString(paramMap));
        List<ChannelDataExportVo> list = sysAnalyzeService.getChannelCountList(paramMap);
        if (list.size() <1)
            return new Result(CodeEnum.FAIL.getCode(),"查询失败");
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),list);
    }

    @PostMapping("/getMerchantDataCount")
    public Result getMerchantDataCount(@RequestBody Map<String,Object> paramMap){
        String startTime = (String) paramMap.get("startTime");
        String endTime = (String) paramMap.get("endTime");
        Long lotteryId =null;
        if(!ObjectUtils.isEmpty(paramMap.get("lotteryId"))){
            lotteryId = Long.parseLong(paramMap.get("lotteryId").toString());
        }
        Long status = Long.parseLong(paramMap.get("status").toString());
        Integer pageNo = Integer.parseInt(paramMap.get("pageNo").toString());
        Integer pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
        Map<String,Object> resultMap = new HashMap<>();
        //数据总的统计量
        Map<String,Object> totalMap = sysAnalyzeService.getMerchantTotalCount();
        resultMap.put("totalCount",totalMap);
        Page<Map<String,Object>> page = new Page<>(pageNo,pageSize);
        List<Map<String,Object>> mapList = sysAnalyzeService.getMerchantUserOrder(startTime,endTime,lotteryId,status,page);
        page.setRecords(mapList);

        resultMap.put("Page",page);
        return new Result(CodeEnum.SUCCESS.getCode(),"统计成功",resultMap);
    }
    @PostMapping("/merchchantConfirmTicketOrCancelOrder")
    public Result merchchantConfirmTicketOrCancelOrder(@RequestParam Long orderId,@RequestParam int status){
        sysAnalyzeService.merchchantConfirmTicketOrCancelOrder(orderId,status);
        return new Result(CodeEnum.SUCCESS.getCode(),"处理成功！");
    }

    @PostMapping("/getWinPrizeData")
    public Result getWinPrizeData(@RequestBody Map<String,Object> paramMap){
        log.info("后台管理中奖数据统计 paramMap={}", JSONObject.toJSONString(paramMap));
        List<Map<String, Object>> winPrizeDataMap = sysAnalyzeService.getWinPrizeData(paramMap);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),winPrizeDataMap);
    }

    @PostMapping("/getAgentDataStatistics")
    public Result getAgentDataStatistics(@RequestBody Map<String,Object> paramMap){
        log.info("代理数据统计 paramMap={}", JSONObject.toJSONString(paramMap));
        Map<String, Object> agentDataMap = sysAnalyzeService.getAgentDataStatistics(paramMap);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),agentDataMap);
    }
    @PostMapping("/getDataProfitCount")
    public Result getDataProfitCount(String startTime, String endTime, Integer timeType){
        Map<String,Object> total = sysAnalyzeService.getTotalProfitCount(startTime,endTime);
        //初始化时间 默认一个月
        if (StrUtils.isEmpty(startTime) || StrUtils.isEmpty(endTime)){
            //初始查询化时间
            endTime = DateUtils.getCurrentDateStr();
            startTime = DateUtils.dateToStr(DateUtils.addDays(new Date(),-30),"yyyy-MM-dd");
        }
        if (total == null)
            total = new HashMap<>();
        List<Map<String,Object>> mapList = sysAnalyzeService.getDataProfitCount(startTime,endTime,timeType);
        total.put("mapList",mapList);
        return new Result(CodeEnum.SUCCESS.getCode(),"统计成功",total);
    }

    /**
    * @Author 苦智
    * @Description 销售漏斗统计
    * @Date 14:57 2018/10/11
    * @Param [startTime, endTime, timetype]
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/saleFunnelCount")
    public Result saleFunnelCount(String startTime,String endTime,Integer timeType,Integer client,Integer pageNo,Integer pageSize){
        log.info("销售漏斗统计参数startTime:" + startTime + ",endTime:" + endTime + ",timetype:" + timeType + ",client:" + client);
        if (StrUtils.isEmpty(startTime) || StrUtils.isEmpty(endTime)){
            //初始查询化时间
            endTime = DateUtils.getCurrentDateStr();
            startTime = DateUtils.dateToStr(DateUtils.addDays(new Date(),-30),"yyyy-MM-dd");
        }
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> totalMap = sysAnalyzeService.getSaleFunnelCountTotal(startTime,endTime,client);
        Page page = new Page(pageNo,pageSize);
        List<Map<String,Object>> list = sysAnalyzeService.getSaleFunnelCountList(page,startTime,endTime,timeType,client);
        page.setRecords(list);
        map.put("total",totalMap);
        map.put("list",page);
        return new Result(CodeEnum.SUCCESS.getCode(),"统计成功",map);
    }

    /**
    * @Author 苦智
    * @Description 用户活跃度分析
    * @Date 9:07 2018/10/13
    * @Param [startTime, endTime, type, source]
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/userActivity")
    public Result userActivityAnalysis(String startTime,String endTime,Integer source,Integer pageNo,Integer pageSize,Integer timeType){
        if (StrUtils.isEmpty(startTime) || StrUtils.isEmpty(endTime)){
            //初始查询化时间
            endTime = DateUtils.getCurrentDateStr();
            startTime = DateUtils.dateToStr(DateUtils.addDays(new Date(),-30),"yyyy-MM-dd");
        }
        Map<String,Object> resultMap = sysAnalyzeService.getUserActivityAnalysis(startTime,endTime,source,pageNo,pageSize,timeType);
        return new Result(CodeEnum.SUCCESS.getCode(),"统计成功",resultMap);
    }

    @PostMapping("/productPerformance")
    public Result productPerformance(String startTime,String endTime,Integer gameType){
        if (StrUtils.isEmpty(startTime) || StrUtils.isEmpty(endTime)){
            startTime = DateUtils.dateToStr(DateUtils.addDays(new Date(),-1),"yyyy-MM-dd");
            endTime = DateUtils.getCurrentDateStr();
        }
        Map<String,Object> resultMap = sysAnalyzeService.productPerformance(startTime,endTime,gameType);
        return new Result(CodeEnum.SUCCESS.getCode(),"统计成功",resultMap);
    }
}
